<?php 
class lcd extends db 
{
	public $dbprefix;
	public $mt_id;

	public function __construct($dbprefix,$mt_id){
		$this->dbprefix = $dbprefix;
		$this->mt_id = $mt_id;
	}


	//球员名字信息等
	public function matchPlayer($mt_id,$rd_id){
		if (!empty($mt_id) && !empty($rd_id)){
			$sql = "SELECT pl_id,pl_cn_name,pl_tw_name,pl_en_name,pl_country,pl_type,pl_sex,mp_class,pl_abbreviation  
					FROM `".$this->dbprefix."player`,`".$this->dbprefix."match_player` 
					WHERE pl_id = mp_player_id AND pl_act = '1' AND mp_match = '".$mt_id."' AND mp_sts = '' ";
			if ($rd_id==2){
				$sql .= " AND mp_notes <> 'R1' ";
			} else if ($rd_id==3){
				$sql .= " AND mp_notes <> 'R1' AND mp_notes <> 'R2' ";
			} else if ($rd_id==4){
				$sql .= " AND mp_notes <> 'R1' AND mp_notes <> 'R2' AND mp_notes <> 'R3' ";
			}
			$res = parent::SelectSQL($sql);
			$num = count($res);
			if ($num>0){
				$k=0;
				foreach ($res as $i => $n){
					$arr['cn_name'][$n[0]] = $n['pl_cn_name'];
					$arr['en_name'][$n[0]] = $n['pl_en_name'];
					$arr['abbreviation'][$n[0]] = $n['pl_abbreviation'];
					$arr['pl_sex'][$n[0]] = $n['pl_sex'];
					$arr['country'][$n[0]] = $n['pl_country'];
					$arr['type'][$n[0]] = $n['pl_type'];
				}
			}
		}
		return $arr;
	}


	public function leaders($mt_id,$rd_id,$leader_number){
		$gp_sql = "SELECT `id`,`mt_group` FROM `".$this->dbprefix."gp_manage` LEFT JOIN `".$this->dbprefix."match` ON `match`.`mt_id`=`gp_manage`.`mt_id`  WHERE `gp_manage`.`mt_id`='".$mt_id."' ";
		$gp_res = parent::SelectSQL($gp_sql);
		$gp_num = count($gp_res);
		
		if($gp_num>0 && $gp_res['mt_group']=='1'){
			$sql = "SELECT a.mp_player_id, a.mp_class, a.pl_cn_name, a.pl_en_name, a.su_rank
							, a.pl_country, a.`name`, a.su_to_par, a.rownum, a.thru
							, a.mp_start_hole, a.mp_end_hole, a.su_rank_code
						FROM (SELECT *
							FROM (SELECT @rownum := @rownum + 1 AS rownum, m.*
								FROM (SELECT @rownum := 0
									) r, (SELECT mp.mp_match, p.pl_cn_name, p.pl_en_name, p.pl_country, ps.su_rank
										, ps.su_rank_code, mp.mp_start_hole, mp.mp_end_hole, mp.mp_player_id, mp.mp_class
										, (SELECT sum(`su_to_par`)  FROM `".$this->dbprefix."player_summary` WHERE `su_mt_id`=$mt_id and `su_pl_id`=mp.mp_player_id) as su_to_par, gp.`name`, (
											SELECT COUNT(*)
											FROM ".$this->dbprefix."player_score
											WHERE sc_mt_id = $mt_id
												AND sc_rd_id = $rd_id
												AND sc_pl_id = mp.mp_player_id
											) AS thru
									FROM ".$this->dbprefix."match_player mp 
									LEFT JOIN (SELECT su_pl_id,CASE WHEN su_rank is null THEN 0 ELSE 1 END AS  is_null,su_rank,su_rank_code,su_to_par FROM ".$this->dbprefix."player_summary where su_mt_id = $mt_id AND su_rd_id = $rd_id ) ps ON mp.mp_player_id = ps.su_pl_id								
									LEFT JOIN ".$this->dbprefix."player p ON mp.mp_player_id = p.pl_id 
									LEFT JOIN ".$this->dbprefix."gp_manage gp ON mp.mp_class = gp.id
									WHERE mp.mp_match = $mt_id
										AND mp.mp_sts =''
									ORDER BY mp.mp_class ASC,ps.is_null DESC, ps.su_rank ASC
									) m
								) aa
							) a LEFT JOIN (SELECT *
							FROM (SELECT @rownum := @rownum + 1 AS rownum, m.*
								FROM (SELECT @rownum := 0
									) r, (SELECT mp.mp_match, ps.su_rank, ps.su_rank_code, mp.mp_player_id, mp.mp_class
										, ps.su_to_par
									FROM ".$this->dbprefix."match_player mp
									LEFT JOIN (SELECT su_pl_id,su_rank,su_rank_code,su_to_par FROM ".$this->dbprefix."player_summary where su_mt_id = $mt_id AND su_rd_id = $rd_id ) ps ON mp.mp_player_id = ps.su_pl_id
									WHERE mp.mp_match = $mt_id
										AND mp.mp_sts =''
									ORDER BY mp.mp_class ASC, ps.su_rank ASC
									) m
								) bb
							) b ON a.mp_class = b.mp_class
						AND a.rownum >= b.rownum
						WHERE a.mp_match = $mt_id
							AND b.mp_match = $mt_id
						GROUP BY a.mp_class, a.rownum ";
			if($gp_num >=7){
				$sql .= "HAVING COUNT(*) <= 1";
			}
			else{
				if($leader_number > 0){
					$sql .= "HAVING COUNT(*) <= ".$leader_number;
				}
				else{
					$sql .= "HAVING COUNT(*) <= 2";
				}
				
			}
						
			$res = parent::SelectSQL($sql);
			$num = count($res);
			
			if ($num>0){
					$k=0;
					foreach ($res as  $n){
					
						$arr[] = $n;
					}
				}elseif ($num <=0 ){
				$sql  = "SELECT  a.mp_player_id, a.mp_class, a.pl_cn_name, a.pl_country, a.`name`
								, a.mp_start_hole, a.mp_end_hole
							FROM (SELECT mp.mp_match, p.pl_cn_name, p.pl_country, mp.mp_start_hole, mp.mp_end_hole
									, mp.mp_player_id, mp.mp_class, gp.`name`
								FROM ".$this->dbprefix."match_player mp LEFT JOIN ".$this->dbprefix."player p ON mp.mp_player_id = p.pl_id LEFT JOIN ".$this->dbprefix."gp_manage gp ON mp.mp_class = gp.id
								WHERE mp.mp_match = $mt_id
								ORDER BY mp.mp_class ASC
								) a LEFT JOIN (SELECT mp.mp_match, mp.mp_class, mp.mp_player_id
								FROM ".$this->dbprefix."match_player mp
								WHERE mp.mp_match = $mt_id
								) b ON a.mp_class = b.mp_class
							AND a.mp_player_id >= b.mp_player_id
							WHERE a.mp_match = $mt_id
								AND b.mp_match = $mt_id
							GROUP BY a.mp_class, a.mp_player_id";
							if($gp_num >=7){
								$sql .= " HAVING COUNT(*) <= 1";
							}
							else{
								if($leader_number > 0){
									$sql .= "HAVING COUNT(*) <= ".$leader_number;
								}
								else{
									$sql .= "HAVING COUNT(*) <= 2";
								}
							}
				$res = parent::SelectSQL($sql);
				$num = count($res);
				foreach ($res as  $n){
					
						$arr[] = $n;
					}
			}
		}else{
		    $sql = "SELECT a.mp_player_id, a.mp_class, a.pl_cn_name, a.pl_en_name, a.su_rank
						, a.pl_country, a.`name`, a.su_to_par, a.rownum, a.thru
						, a.mp_start_hole, a.mp_end_hole, a.su_rank_code
					FROM (SELECT *
						FROM (SELECT @rownum := @rownum + 1 AS rownum, m.*
							FROM (SELECT @rownum := 0
								) r, (SELECT mp.mp_match, p.pl_cn_name, p.pl_en_name, p.pl_country, ps.su_rank
									, ps.su_rank_code, mp.mp_start_hole, mp.mp_end_hole, mp.mp_player_id, mp.mp_class
									, (SELECT sum(`su_to_par`)  FROM `".$this->dbprefix."player_summary` WHERE `su_mt_id`=$mt_id and `su_pl_id`=mp.mp_player_id) as su_to_par, gp.`name`, (
										SELECT COUNT(*)
										FROM ".$this->dbprefix."player_score
										WHERE sc_mt_id = $mt_id
											AND sc_rd_id = $rd_id
											AND sc_pl_id = mp.mp_player_id
										) AS thru
								FROM ".$this->dbprefix."match_player mp LEFT JOIN ".$this->dbprefix."player_summary ps ON mp.mp_player_id = ps.su_pl_id LEFT JOIN ".$this->dbprefix."player p ON mp.mp_player_id = p.pl_id LEFT JOIN ".$this->dbprefix."gp_manage gp ON mp.mp_class = gp.id
								WHERE mp.mp_match = $mt_id
									AND ps.su_mt_id = $mt_id
									AND ps.su_rd_id = $rd_id
									AND mp.mp_sts=''
								ORDER BY mp.mp_class ASC, ps.su_rank ASC
								) m
							) aa
						) a LEFT JOIN (SELECT *
						FROM (SELECT @rownum := @rownum + 1 AS rownum, m.*
							FROM (SELECT @rownum := 0
								) r, (SELECT mp.mp_match, ps.su_rank, ps.su_rank_code, mp.mp_player_id, mp.mp_class,mp.mp_sts
									, ps.su_to_par
								FROM ".$this->dbprefix."match_player mp LEFT JOIN ".$this->dbprefix."player_summary ps ON mp.mp_player_id = ps.su_pl_id
								WHERE mp.mp_match = $mt_id
									AND ps.su_mt_id = $mt_id
									AND ps.su_rd_id = $rd_id
									AND mp.mp_sts=''
								ORDER BY mp.mp_class ASC, ps.su_rank ASC
								) m
							) bb
						) b ON a.mp_class = b.mp_class
					AND a.rownum >= b.rownum
					WHERE a.mp_match = $mt_id
						AND b.mp_match = $mt_id
					GROUP BY a.mp_class, a.rownum";
					
			if($leader_number > 0){
				$sql .= " HAVING COUNT(*) <= ".$leader_number;
			} else{
				$sql .= " HAVING COUNT(*) <= 18";
			}

			$res = parent::SelectSQL($sql);
			$num = count($res);
			
			if ($num>0){
					$k=0;
					foreach ($res as  $n){
					
						$arr[] = $n;
					}
				}elseif ($num <=0 ){
				$sql  = "SELECT  a.mp_player_id, a.mp_class, a.pl_cn_name,a.pl_en_name, a.pl_country, a.`name`
								, a.mp_start_hole, a.mp_end_hole
							FROM (SELECT mp.mp_match, p.pl_cn_name,p.pl_en_name, p.pl_country, mp.mp_start_hole, mp.mp_end_hole
									, mp.mp_player_id, mp.mp_class, gp.`name`
								FROM ".$this->dbprefix."match_player mp LEFT JOIN ".$this->dbprefix."player p ON mp.mp_player_id = p.pl_id LEFT JOIN ".$this->dbprefix."gp_manage gp ON mp.mp_class = gp.id
								WHERE mp.mp_match = $mt_id
								AND mp_sts =''
								ORDER BY mp.mp_class ASC
								) a LEFT JOIN (SELECT mp.mp_match, mp.mp_class, mp.mp_player_id
								FROM ".$this->dbprefix."match_player mp
								WHERE mp.mp_match = $mt_id
								      AND mp_sts =''
								) b ON a.mp_class = b.mp_class
							AND a.mp_player_id >= b.mp_player_id
							WHERE a.mp_match = $mt_id
								AND b.mp_match = $mt_id
							GROUP BY a.mp_class, a.mp_player_id ";
				if($leader_number > 0){
					$sql .= " HAVING COUNT(*) <= ".$leader_number;
				}
				else{
					$sql .= " HAVING COUNT(*) <= 2";
				}
				$res = parent::SelectSQL($sql);
				$num = count($res);
				foreach ($res as  $n){
					
						$arr[] = $n;
					}
			}
		}
		return $arr;
	} 


		//summary排名
	public function rankSummary($mt_id,$rd_id,$offet,$limit,$mt_group,$TopGD,$lcd_Rand){
		if (!empty($mt_id) && !empty($rd_id)){
			//if ($lcd_Rand=='2'){
			$holecount = ",(SELECT COUNT(*) FROM ".$this->dbprefix."player_score WHERE sc_pl_id=mp_player_id AND sc_mt_id='".$mt_id."' AND sc_rd_id='".$rd_id."' AND sc_type='M' AND sc_sts!='R' ) AS holecount ";
			//}
			//按分组分参赛球员
			$mp_class_pl = array();
			$mp_class_sql = "SELECT mp_class,mp_player_id FROM ".$this->dbprefix."match_player WHERE  mp_match='".$mt_id."'  AND mp_sts='' ";//排除掉已弃赛选手
			$mp_class_res = mysql_query($mp_class_sql);
			$mp_class_num = mysql_num_rows($mp_class_res);
			if($mp_class_num > 0){
				for($i=0 ;$i<$mp_class_num ;$i++){
					$mp_class_row = mysql_fetch_array($mp_class_res);
					$mp_class_pl[$mp_class_row['mp_class']][] = $mp_class_row['mp_player_id'];
				}
			}
			foreach($mp_class_pl as $clkey=>$clval){//按组计算
				$pl_cal_arr = '';
				$len = count($clval);
				for($kkk=0 ; $kkk<$len; $kkk++){
					if(($kkk+1) < $len){
						$pl_cal_arr .= $clval[$kkk].',';
					}
					else{
						$pl_cal_arr .= $clval[$kkk];
					}
				}
				$sql = "SELECT mp_player_id, mp_sts, su_score, su_to_par, su_rank, su_rank_code, su_sts, mp_type ".$holecount."
						FROM ".$this->dbprefix."match_player LEFT JOIN ".$this->dbprefix."player_summary 
						ON mp_player_id = su_pl_id AND mp_match = su_mt_id AND su_rd_id = '".$rd_id."' ";
				$sql .= "WHERE mp_match = '".$mt_id."' AND mp_sts = '' ";
				if (!empty($mt_group)) $sql .= " AND mp_class = '".$mt_group."' ";
				if($rd_id == 2){
					$sql .= "AND mp_notes <> 'R1' ";
				} else if ($rd_id == 3) {
					$sql .= "AND mp_notes <> 'R1' AND mp_notes <> 'R2' ";
				} else if ($rd_id == 4){
					$sql .= "AND mp_notes <> 'R1' AND mp_notes <> 'R2' AND mp_notes <> 'R3' ";
				}
				$SortRank = ($lcd_Rand=='2') ? 'holecount DESC,su_net' : 'su_rank,holecount DESC';
				$sql .= "ORDER BY su_cdate DESC,".$SortRank." , su_pl_id DESC ";
				$sql .= "LIMIT ".$offet.", ".$limit;
				#echo $sql;
				#exit();
				$res = parent::SelectSQL($sql);
				$num = count($res);
				if ($num>0){
					$basic = ($TopGD==0) ? 1 : (1+$TopGD);
					$k = $basic;
					foreach ($res as $i => $n){	
						//if (!empty($mt_group)){
							//$arr['rank'][$n[0]] = ($offet>$TopGD) ? ($offet-$limit+$k) : $k;
						//} else {
							$arr['rank'][$n[0]] = (empty($n[5])) ? $n[4] : (($n[5]=='T') ? 'T'.$n[4] : $n[5]);
						//}
						$arr['check'][$n[0]] = $n[6];
						$k++;
					}
				}	
		    }
		}
		return $arr;
	}

	//get player F
	public function CheckPlayerF($mt_id,$rd_id){
		if (!empty($mt_id) && !empty($rd_id)){
			$sql = "SELECT sc_pl_id,COUNT(*) FROM ".$this->dbprefix."player_score 
					WHERE sc_mt_id='".$mt_id."' AND sc_rd_id = '".$rd_id."' AND sc_type='M' AND sc_sts!='R' 
					GROUP BY sc_pl_id";
			$res = parent::SelectSQL($sql);
			$num = count($res);
			if ($num>0){
				foreach ($res as $i => $n){
					$arr[$n[0]] = $n[1];
				}
			}
		}
		return $arr;
	}

	public function totalHole($mt_id,$rd_id){
		$sql = "SELECT COUNT(sc_ho_id),sc_pl_id FROM ".$this->dbprefix."player_score 
				WHERE sc_mt_id =  '".$mt_id."' AND sc_rd_id = '".$rd_id."' GROUP BY sc_pl_id";		
		$res = parent::SelectSQL($sql);
		$num = count($res);
		if ($num>0){
			foreach ($res as $i => $n){
				$arr['thru'][$n[1]] = $n[0]; 
			}
		}
		return $arr;
	}	

	//开球球洞起始
	public function startHole($mt_id,$rd_id){
		if (!empty($mt_id) && !empty($rd_id)){
			$sql = "SELECT gpd_player, gp_start_hole FROM `".$this->dbprefix."group`, `".$this->dbprefix."group_detail` 
					WHERE gp_id = gpd_id AND gp_mt_id = '".$mt_id."' AND gp_rd_id = '".$rd_id."' ";
			$res = parent::SelectSQL($sql);
			$num = count($res);
			if ($num>0){
				foreach ($res as $i => $n){
					$arr[$n[0]] = $n[1];
				}	
			}
		}
		return $arr;
	}

	public function getTotal($mt_id,$rd_id,$mt_group=''){
		if (!empty($mt_id) && !empty($rd_id)){
			$sql = "SELECT COUNT(mp_player_id) AS total FROM ".$this->dbprefix."match_player WHERE mp_match = '".$mt_id."' AND mp_sts = '' ";
			if (!empty($mt_group)) $sql .= " AND mp_class = '".$mt_group."' ";
			if($rd_id == 2){
				$sql .= "AND mp_notes <> 'R1' ";
			} else if ($rd_id == 3){
				$sql .= "AND mp_notes <> 'R1' AND mp_notes <> 'R2' ";
			} else if ($rd_id == 4){
				$sql .= "AND mp_notes <> 'R1' AND mp_notes <> 'R2' AND mp_notes <> 'R3' ";
			}
			$res = parent::SelectSQL($sql);
			return $res[0][0];
		}
	}

	public function getTeamTotal($mt_id,$rd_id,$mt_tmgroup=''){
		if (!empty($mt_id) && !empty($rd_id)){
			$sql = "SELECT COUNT(id) AS total FROM `".$this->dbprefix."team` WHERE mt_id = '".$mt_id."' AND act = '1' ";
			#$sql .= " AND `group` = '".$mt_tmgroup."' ";
			$res = parent::SelectSQL($sql);
			return $res[0][0];
		}
	}

	//get Round topar
	public function RoundScore($mt_id){
		if (!empty($mt_id)){
			$sql = "SELECT su_pl_id, su_rd_id, su_score, su_to_par, su_net  
					FROM ".$this->dbprefix."player_summary 
					WHERE su_mt_id = '".$mt_id."' ORDER BY su_pl_id, su_rd_id ";
			$res = parent::SelectSQL($sql);
			$num = count($res);
			if ($num>0){
				foreach ($res as $i => $n){
					$arr['score'][$n[0]][$n[1]] = $n[2];
					$arr['topar'][$n[0]][$n[1]] = $n[3];
					$arr['net'][$n[0]][$n[1]] = $n[4];
				}
			}
		}
		return $arr;
	}

	//球员的逐洞成绩
	public function EveryHoleScore($mt_id,$rd_id){
		global $matchTeam;
		if (!empty($mt_id) && !empty($rd_id)){
			$sql = "SELECT sc_pl_id,sc_ho_id,sc_score,sc_to_par,sc_point,sc_net FROM ".$this->dbprefix."player_score
					WHERE sc_mt_id = '".$mt_id."' AND sc_rd_id = '".$rd_id."' AND sc_type = 'M'  order by sc_ho_id ASC";			
			$res = parent::SelectSQL($sql);
			$num = count($res);
			if ($num>0){
				foreach ($res as $i => $n){
					if ($n[3]<='-2'){
						$toClass = ($matchTeam=='1') ? 'two_eagle' : 'eagle';
					} else if ($n[3]=='-1'){
						$toClass = 'birdie';
					} else if ($n[3]=='0'){
						$toClass = 'par';
					} else if ($n[3]=='1'){
						$toClass = 'bogey';
					} else if ($n[3]>='2'){
						$toClass = 'dbogey';
					} else {
						$toClass = 'par';
					}
				
					$arr['score'][$n[0]][$n[1]] = $n[2];
					$arr['topar'][$n[0]][$n[1]] = $n[3];
					$arr['point'][$n[0]][$n[1]] = $n[4];
					$arr['net'][$n[0]][$n[1]] = $n[5];
					$arr['class'][$n[0]][$n[1]] = $toClass;
				}
			}
		} 
		return $arr;
	}

	//读取上一轮的排名
	function playerPrevSummary($mt_id,$rd_id,$mt_group=""){
		global $maxRound;
		if (!empty($mt_id) && $rd_id>1){
			$round_preId = $rd_id-1;
			$sql = "SELECT mp_player_id AS mp_pl_id,su_rank,su_rank_code,mp_sts,mp_notes,su_new_total,su_hdcp,su_net,CASE WHEN su_rank is null THEN 0 ELSE 1 END AS  is_null
					FROM ".$this->dbprefix."match_player LEFT JOIN ".$this->dbprefix."player_summary 
					ON mp_player_id = su_pl_id AND mp_match = su_mt_id AND su_rd_id = '".$round_preId."'
					WHERE mp_match = '".$mt_id."' AND mp_sts='' ";
			if (is_numeric($mt_group)) $sql .= " AND mp_class = '".$mt_group."' ";		
			if($round_preId == 1) {
				$sql .= "AND mp_notes <> 'R1' ";
			}
			if($round_preId == 2) {
				$sql .= "AND mp_notes <> 'R1' AND mp_notes <> 'R2' ";
			}
			if($round_preId == 3) {
				$sql .= "AND mp_notes not in ('R1','R2','R3') ";
			}
			$sql .= "ORDER BY  is_null DESC, su_rank, su_pl_id ";
			$res = parent::SelectSQL($sql);
			$num = count($res);
			if ($num>0){
				foreach ($res as $i => $n){
					if($n['mp_sts'] != '' and $n['mp_notes']=='R'.$rd_id){
						$arr['rank'][$n['mp_pl_id']] = $n['mp_sts'];
					} else {
						if($n['su_rank_code'] == ''){
							$arr['rank'][$n['mp_pl_id']] = $n['su_rank'];
						} else if ($n['su_rank_code'] == 'T'){
							$arr['rank'][$n['mp_pl_id']] = 'T'.$n['su_rank'];
						} else {
							$arr['rank'][$n['mp_pl_id']] = $n['mp_sts'];
						}
					}
					$arr['checked'][$n['mp_pl_id']] = $n['su_sts'];
					$arr['nnp'][$n['mp_pl_id']] = $n['su_new_total'];
					$arr['hdcp'][$n['mp_pl_id']] = $n['su_hdcp'];
					$arr['net'][$n['mp_pl_id']] = $n['su_net'];
					if ($n['su_rank'] == 1 && $rd_id == $maxRound) $arr['notes'][$n['mp_pl_id']] = $n['mp_notes'];
				}
			}
		}
		return $arr;
	}

	//读取团队赛分组
	public function getTMMatchGroup($mt_id,$rd_id){
		if (!empty($mt_id)){
			$sql = "SELECT `group` FROM `".$this->dbprefix."team` WHERE mt_id = '".$mt_id."' AND act = '1' GROUP BY `group` ORDER BY `group` DESC";
			$res = parent::SelectSQL($sql);
			$num = count($res);
			if ($num>0){
				foreach ($res as $i => $n){	
					$tm_matchGroup[] = $n[0];
				}	
			} else {
				$tm_matchGroup = '';
			}
			return $tm_matchGroup;
		}
	}

	//团队赛成绩--用户取和用
	public function teamPlayerAcc($mt_id,$rd_id){
		if (!empty($mt_id)){
			$sql = "SELECT a.tp_tm_id, b.pl_cn_name,a.tp_pl_id FROM ".$this->dbprefix."team_player AS a
					LEFT JOIN `".$this->dbprefix."player` AS b ON a.tp_pl_id = b.pl_id 
					LEFT JOIN `".$this->dbprefix."team` AS c ON a.tp_tm_id = c.id 
					WHERE c.mt_id = '".$mt_id."'";
			$res = parent::SelectSQL($sql);
			$num = count($res);
			if ($num>0){
				foreach ($res as $i => $n){
					$arr['pl_name'][$n[0]][$n[2]] = $n[1];
					$arr['pl_id'][$n[0]][$n[2]] = $n[2];
					$arr['pl_num'][$n[0]]++;
				}
			}
			return $arr;		
		}
	}

	//读取团队成绩的排名
	public function teamSummary($mt_id,$rd_id,$mt_tmgroup=''){
		if (empty($mt_tmgroup)) $mt_tmgroup = 0;
		if (!empty($mt_id) && !empty($rd_id)){
			$sql = "SELECT `id`,`team_ch_name` FROM `".$this->dbprefix."team` WHERE mt_id = '".$mt_id."' AND `act` = '1' AND `group` = '".$mt_tmgroup."' ";
			$res = parent::SelectSQL($sql);
			$num = count($res);
			if ($num>0){
				foreach ($res as $i => $n){
					$rank_sql = "SELECT tu_tm_id, tu_sts, tu_score, tu_rank, tu_rank_code,tu_cuser FROM ".$this->dbprefix."team_summary WHERE tu_tm_id = '".$n['id']."' AND tu_mt_id = '".$mt_id."' AND tu_rd_id = '".$rd_id."' AND tu_type = '' ";
					if($round_id == 2){
						$rank_sql .= "AND tu_notes <> 'R1' ";
					} else if ($round_id == 3) {
						$rank_sql .= "AND tu_notes <> 'R1' AND tu_notes <> 'R2' ";
					} else if ($round_id == 4){
						$rank_sql .= "AND tu_notes <> 'R1' AND tu_notes <> 'R2' AND tu_notes <> 'R3' ";
					}
					$rank_res = parent::SelectSQL($rank_sql);
					$rank_num = count($rank_res);
					if ($rank_num>0){
						$arr[$n[0]]['rank'] = (empty($rank_res[0]['tu_cuser'])) ? $rank_res[0]['tu_rank'] : $rank_res[0]['tu_cuser'];
						$arr[$n[0]]['tm_name'] = $n['team_ch_name'];
					} else {
						$arr[$n[0]]['rank'] = '';
						$arr[$n[0]]['tm_name'] = $n['team_ch_name'];
					}
				}
			}
		}
		return $arr;
	}

	public function getTeamPlayer($mt_id,$rd_id){
		global $tmType;
		$tot = (empty($tmType)) ? 4 :$tmType;
		if (!empty($mt_id)){
			$sql = "SELECT a.tp_tm_id, b.pl_cn_name,c.gpd_line,a.tp_pl_id,c.gpd_id,b.pl_en_name,b.pl_country FROM ".$this->dbprefix."team_player AS a
					LEFT JOIN ".$this->dbprefix."player AS b ON a.tp_pl_id = b.pl_id
					LEFT JOIN (SELECT gpd_player,gpd_line,gpd_id FROM `".$this->dbprefix."group`,`".$this->dbprefix."group_detail` WHERE gp_id = gpd_id AND gp_mt_id = '".$mt_id."' AND gp_rd_id = '".$rd_id."') AS c ON a.tp_pl_id = c.gpd_player WHERE a.tp_notes = '".$mt_id."' ORDER BY a.tp_tm_id ASC,c.gpd_id ASC,c.gpd_line ASC ";
			$res = parent::SelectSQL($sql);
			$num = count($res);
			if ($num>0){
				$k=1;
				foreach ($res as $i => $n){
					if ($k==1){
						$one = $n[0];
					}
					
					$arr['cn_name'][$n[0]][$k] = $n[1];
					$arr['en_name'][$n[0]][$k] = $n[5];
					$arr['pl_id'][$n[0]][$k] = $n[3];
					$arr['pl_country'][$n[0]][$k] = $n[6];
					if ($one==$n[0]){
						if ($k<$tot){
							$k++;	
						} else {
							$k=1;
						}
					} else {
						$k=1;
					}
				}
			}
		}
		return $arr;
	}

	//团队赛TOTAL总成绩
	public function hyh_TeamTotal($mt_id){
		$sql = "SELECT tu_tm_id,tu_rd_id,tu_to_par,tu_score FROM ".$this->dbprefix."team_summary WHERE tu_mt_id = '".$mt_id."' ";
		$res = parent::SelectSQL($sql);
		$num = count($res);
		if ($num>0){
			foreach ($res as $i => $n){
				$arr['tm_score'][$n[0]][$n[1]] = $n[3];
				$arr['tm_today'][$n[0]][$n[1]] = $n[2];
				$arr['tm_topar'][$n[0]] += $n[2];	
			}
		}
		return $arr;
	}

	//团队成绩中每一队的成绩
	public function CompareScore($sc1,$sc2,$par){
		for ($a=1;$a<=18;$a++){
			if ($sc1[$a]>$sc2[$a]){
				$score = $sc2[$a];
			} else {
				$score = $sc1[$a];
			}
			$topar = $score-$par[$a-1];
			if ($topar<='-2'){
				$toClass = 'eagle';
			} else if ($topar=='-1'){
				$toClass = 'birdie';
			} else if ($topar=='0'){
				$toClass = 'par';
			} else if ($topar=='1'){
				$toClass = 'bogey';
			} else if ($topar>='2'){
				$toClass = 'db_bogey';
			} else {
				$toClass = 'par';
			}
			$arr['score'][$a-1] = $score;
			$arr['class'][$a-1] = $toClass;
		}
		return $arr;
	}
}
?>